package com.ygxsk.carhome.ui.adapter;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ClickUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.listener.MyArchiveFileAddSubmitListener;
import com.ygxsk.carhome.response.AgreenMentFileAddSubmit;
import com.ygxsk.carhome.response.EvaluateWadeInfo;
import com.ygxsk.carhome.ui.archive.ArchiveFileAddSubmitActivity;
import com.ygxsk.carhome.utils.CommUtils;
import com.ygxsk.carhome.views.ClearEditText;
import com.zhongjh.progresslibrary.widget.MaskProgressLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Autuhor:itchen
 * Time:2019/11/14 0014  14:37
 * Description: this is 退货详情之中的适配器内容
 */
public class AgreenMentFileAdapter extends BaseQuickAdapter<AgreenMentFileAddSubmit, BaseViewHolder> {

    //该页面的监听器:例如选择类型，选择日期，修改条目之中edit，删除这个条目；
    private MyArchiveFileAddSubmitListener myArchiveFileAddSubmitListener;

    public void setMyArchiveFileAddSubmitListener(MyArchiveFileAddSubmitListener _myArchiveFileAddSubmitListener) {
        myArchiveFileAddSubmitListener = _myArchiveFileAddSubmitListener;
    }

    private ArchiveFileAddSubmitActivity.MyMaskProgressLayoutListener myMaskProgressLayoutListener;

    public void setMyMaskProgressLayoutListener(ArchiveFileAddSubmitActivity.MyMaskProgressLayoutListener _myMaskProgressLayoutListener) {
        this.myMaskProgressLayoutListener = _myMaskProgressLayoutListener;
    }

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param layoutResId The layout resource id of each item.
     * @param data        A new list is created out of this one to avoid mutable list
     */
    public AgreenMentFileAdapter(int layoutResId, @Nullable List<AgreenMentFileAddSubmit> data) {
        super(layoutResId, data);
    }


    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * @param helper                  A fully initialized helper.
     * @param agreenMentFileAddSubmit The item that needs to be displayed.
     */
    @Override
    protected void convert(BaseViewHolder helper, AgreenMentFileAddSubmit agreenMentFileAddSubmit) {

        if (!ObjectUtils.isEmpty(agreenMentFileAddSubmit)) {

            //一个单独的id 可能有值，可能没有值；
//            int id = agreenMentFileAddSubmit.getId();

            //编号
            int identifierNumber = agreenMentFileAddSubmit.getIdentifierNumber();

            //类型-合同类型
            String agreenType = agreenMentFileAddSubmit.getAgreenType();

            //选中的这条 合同类型的整块
            EvaluateWadeInfo evaluateWadeInfo = agreenMentFileAddSubmit.getEvaluateWadeInfo();

            //签约时间
            String agreenTime = agreenMentFileAddSubmit.getAgreenTime();

            //签约人
            String agreenContactPerson = agreenMentFileAddSubmit.getAgreenContactPerson();

            //签约的金额
//            String agreenAmount = agreenMentFileAddSubmit.getAgreenAmountBigDecimalString();

            //签约的金额 数字格式；
            //BigDecimal agreenAmountBigDecimal = agreenMentFileAddSubmit.getAgreenAmountBigDecimal();
            //签约的金额 文本格式
            String agreenAmountBigDecimalString = agreenMentFileAddSubmit.getAgreenAmountBigDecimalString();

//            String agreementPriceFinal = "";
//            if (agreenAmountBigDecimal != null) {
//                agreementPriceFinal = MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL, StringUtils.getString(R.string.yangjiaocurrencysymbol)) + agreenAmountBigDecimal.toString();
//            } else {
//                agreementPriceFinal = MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL, StringUtils.getString(R.string.yangjiaocurrencysymbol)) + CommUtils.formatComma2BigDecimal(CommUtils.valueOf(0.00));
//            }

            //查找id的内容
            //合同的类型的外侧包裹内容
            LinearLayout layoutFileAgreenMentTypeAdapter = (LinearLayout) helper.getView(R.id.layoutfileagreenmenttypeadapter);
            //合同信息-标志顶部的一个叉号  关闭按钮：
            ImageView imageViewDeleteInMiddleRight = (ImageView) helper.getView(R.id.imageviewdeleteinmiddleright);
            //合同的类型的文本填充
            TextView textViewFileAgreenNameAdapter = (TextView) helper.getView(R.id.textviewfileagreennameadapter);
            //合同签约时间的外侧点击事件；
            RelativeLayout layoutAgreenTimeOutSideAdapter = (RelativeLayout) helper.getView(R.id.layoutagreentimeoutsideadapter);
            //合同签约时间的文本填充
            TextView textViewAgreenTimeAdapter = (TextView) helper.getView(R.id.textviewagreentimeadapter);
            //合同签约人，文本输入
            ClearEditText editViewNameAgreenAdapter = (ClearEditText) helper.getView(R.id.editviewnameagreenadapter);
            //合同签约的 价格信息
            ClearEditText editViewPriceInAgreen = (ClearEditText) helper.getView(R.id.editviewpriceinagreen);

            //填充附件图片的内容
            MaskProgressLayout mplimageListInAgreenMentAdapter = (MaskProgressLayout) helper.findView(R.id.mplimagelistinagreenmentadapter);
            mplimageListInAgreenMentAdapter.setOperation(true);
            agreenMentFileAddSubmit.setMaskProgressLayout(mplimageListInAgreenMentAdapter);

            if (mplimageListInAgreenMentAdapter != null) {
                //对图片的设置九宫格内容图片的查看，默认新增一个就是空白的图片选择器才对
                mplimageListInAgreenMentAdapter.setMaskProgressLayoutListener(myMaskProgressLayoutListener);
                List<String> imageList = new ArrayList<>();
                //首次新增必须是空的图片
                mplimageListInAgreenMentAdapter.setImageUrls(imageList);

                myMaskProgressLayoutListener.setModifyMiddleTallAddSubmit(agreenMentFileAddSubmit);
            }

            //-----------------------------------------------------------------------------------------------------------------------------------------------
            //判断如果是第一条数据，那么不展示关闭按钮；
            int indexPosition = getItemPosition(agreenMentFileAddSubmit);

            if (indexPosition == 0) {
                imageViewDeleteInMiddleRight.setVisibility(View.GONE);
            } else {
                imageViewDeleteInMiddleRight.setVisibility(View.VISIBLE);
            }

            //合同类型设置新值
            CommUtils.setText(textViewFileAgreenNameAdapter, agreenType);

            //签约时间设置新值
            CommUtils.setText(textViewAgreenTimeAdapter, agreenTime);


            //-----------------------------------------------------------------------------------------------------------------------------------------------
            //签约人的监听器
            TextWatcher agreementPersonNameTextWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable seditable) {
                    String input = seditable.toString();
                    if (!TextUtils.isEmpty(input)) {

                        if (myArchiveFileAddSubmitListener != null) {
                            if (agreenMentFileAddSubmit != null) {
                                myArchiveFileAddSubmitListener.modifyAgreeMentPersonName(identifierNumber, input, agreenMentFileAddSubmit);
                            } else {
                                ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
                            }
                        }

                    } else {
//                        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "监听的--资产名称 文本为空");
                    }
                }
            };

            if ((editViewNameAgreenAdapter.getTag() != null) && (editViewNameAgreenAdapter.getTag() instanceof TextWatcher)) {
                //LogUtils.d(ConstantSign.LOGCAT_NOMAL + "removeTextChangedListener--removeTextChangedListener--removeTextChangedListener");
                editViewNameAgreenAdapter.removeTextChangedListener((TextWatcher) editViewNameAgreenAdapter.getTag());
            }

            //移除之后设置名称
            editViewNameAgreenAdapter.setText(agreenContactPerson);
            //添加监听
            editViewNameAgreenAdapter.addTextChangedListener(agreementPersonNameTextWatcher);
            //设置标记,为了上面删除监听使用
            editViewNameAgreenAdapter.setTag(agreementPersonNameTextWatcher);

            //-----------------------------------------------------------------------------------------------------------------------------------------------
            //监听输入的合同金额
            TextWatcher agreementPriceTextWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable seditable) {
                    String input = seditable.toString();
                    if (!TextUtils.isEmpty(input)) {

                        if (myArchiveFileAddSubmitListener != null) {
                            if (agreenMentFileAddSubmit != null) {
                                myArchiveFileAddSubmitListener.modifyAgreementPriceAmont(identifierNumber, input, agreenMentFileAddSubmit);
                            } else {
                                ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
                            }
                        }

                    } else {
//                        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "监听的--资产名称 文本为空");
                    }
                }
            };

            if ((editViewPriceInAgreen.getTag() != null) && (editViewPriceInAgreen.getTag() instanceof TextWatcher)) {
                //LogUtils.d(ConstantSign.LOGCAT_NOMAL + "removeTextChangedListener--removeTextChangedListener--removeTextChangedListener");
                editViewPriceInAgreen.removeTextChangedListener((TextWatcher) editViewPriceInAgreen.getTag());
            }

            //移除之后设置名称 : 合同添加的价格
            editViewPriceInAgreen.setText(agreenAmountBigDecimalString);
            //添加监听
            editViewPriceInAgreen.addTextChangedListener(agreementPriceTextWatcher);
            //设置标记,为了上面删除监听使用
            editViewPriceInAgreen.setTag(agreementPriceTextWatcher);

            //-----------------------------------------------------------------------------------------------------------------------------------------------

            //选择合同类型
            if (layoutFileAgreenMentTypeAdapter != null) {
                layoutFileAgreenMentTypeAdapter.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                    @Override
                    public void onDebouncingClick(View v) {
                        if (myArchiveFileAddSubmitListener != null) {
                            myArchiveFileAddSubmitListener.chooseAgreementType(identifierNumber, evaluateWadeInfo, agreenMentFileAddSubmit);
                        }
                    }
                });
            }

            //选择合同的时间
            if (layoutAgreenTimeOutSideAdapter != null) {
                layoutAgreenTimeOutSideAdapter.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                    @Override
                    public void onDebouncingClick(View v) {
                        if (myArchiveFileAddSubmitListener != null) {
                            myArchiveFileAddSubmitListener.chooseAgreementTime(identifierNumber, agreenMentFileAddSubmit);
                        }
                    }
                });
            }

            if (imageViewDeleteInMiddleRight != null) {
                imageViewDeleteInMiddleRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (myArchiveFileAddSubmitListener != null) {
                            myArchiveFileAddSubmitListener.modifyDeleteCurrentItem(identifierNumber, agreenMentFileAddSubmit);
                        }

                    }
                });
            }
            //-----------------------------------------------------------------------------------------------------------------------------------------------

        }
    }
}
